//
//  SWLoginController.swift
//  SinaWeibo
//
//  Created by wszyxc on 16/7/20.
//  Copyright © 2016年 wszyxc. All rights reserved.
//

import UIKit

class SWLoginController: SWBaseController {
    
    
    override func loadView() {
        
        view = webView
  
        getCode()
        
        webView.delegate = self
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupNav()
    
    }
    
    func getCode(){
        
        let url:NSURL = (NSURL(string: "https://api.weibo.com/oauth2/authorize?client_id=274585185&redirect_uri=http://www.baidu.com"))!
        
        let request:NSURLRequest = NSURLRequest(URL: url)
        
        webView.loadRequest(request)
        
        
    }
    
    
    func setupNav(){
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: nil, title: "取消", target: self, action: "cancelLoginClick")
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: nil, title: "自动填充", target: self, action: "autoFillClick")
        
    }
    
    func cancelLoginClick(){
        
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    func autoFillClick(){
        
        print("自动填充")
        
        let jsString = "document.getElementById('userId').value='\(wbName)',document.getElementById('passwd').value='\(wbPasswd)'"
        //js注入
        webView.stringByEvaluatingJavaScriptFromString(jsString)
    }
    
    
    
    //懒加载控件
    
    lazy var webView:UIWebView = UIWebView()

}


// MARK: - UIWebViewDelegate
extension SWLoginController:UIWebViewDelegate{
    /*
    监听webView 将要加载的request
    - 其有返回值
    - 如果返回为true 代表继续加载
    - 如果返回为false 带包要停止加载
    - 默认如果不实现该方法 默认为true
    
    */    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool{
        
        //****在这里需要得到code,然后用code获取accesToken*****//
    
        // 请求的url字符串是否为nil,且他的前缀是否是回调页
         let urlString = request.URL?.absoluteString //获取到urlStr
        
        if let urlStr = urlString where urlStr.hasPrefix(APPREDIRECT_URI){
            //获取url的请求参数query
            
            if let query = request.URL?.query {
                
                //如果query不为空,获取code(截取字符串)
                print("----",query)
                
                let code = query.substringFromIndex("code=".endIndex)
                
                print(code)
                
                //拿到code 去请求数据
                
                SWUserAcountViewModel.sharedUserAcountViewModel.loadUserAcount(code, finished: { (isSuccess) -> () in
                    
                    print(isSuccess)
                    
                })
                
            }
            
            
        }
        
        return true
    }

    func webViewDidStartLoad(webView: UIWebView){
        
        print("webview开始加载")
    }
  
    func webViewDidFinishLoad(webView: UIWebView){
        print("webview结束加载")
    }

    func webView(webView: UIWebView, didFailLoadWithError error: NSError?){
        print("下载错误\(error)")
    }
    
}

// MARK: - 请求accesToken 和获取 userInfo


